//
//  RecommendCell.m
//  Project
//
//  Created by liting on 2025/9/4.
//

#import "RecommendCell.h"
#import "Masonry.h"
#import "RecommendModel.h"
#import <SDWebImage/SDWebImage.h>

@interface RecommendCell ()

@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *authorLable;
@property (nonatomic, strong) UILabel *titleLabel;

@end


@implementation RecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.img = [[UIImageView alloc] init];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    self.img.layer.cornerRadius = 25;
    self.img.layer.masksToBounds = YES;
    self.img.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.img];
    
    self.authorLable = [[UILabel alloc] init];
    self.authorLable.font = [UIFont systemFontOfSize:14];
    self.authorLable.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.authorLable];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.titleLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.titleLabel];
    
    [self setupConstraints];
}

- (void)setupConstraints {
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(50);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.img.mas_right).offset(12);
        make.top.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-12);
    }];
    
    [self.authorLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.img.mas_right).offset(12);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.right.equalTo(self.contentView).offset(-12);
    }];
}



- (void)configureWithModel:(RecommendModel *)model{
    self.titleLabel.text = model.title;
    self.authorLable.text = [NSString stringWithFormat:@"作者: %@",model.author.name];
    
    // 加载图片
    if (model.cover.v_cover) {
        [self.img sd_setImageWithURL:[NSURL URLWithString:model.cover.v_cover]
                    placeholderImage:[UIImage imageNamed:@"placeholder"]];
    } else {
        self.imageView.image = [UIImage imageNamed:@"placeholder"];
    }
}

@end
